import { HttpCacheService } from '@core/net/http-cache.service';
/* API服务示例 */
import { Injectable } from '@angular/core';
import { ApiService } from '@core/net/api.service';
import { IHttpRes } from '@core/net/http-response';
import { USER_AUTH, USER_INFO, USER_LOGIN } from '@common/static';
import { ILogin } from '@model/login';



@Injectable({
	providedIn: "root"
})
export class LoginService {

	constructor(
		private apiService: ApiService,
		private httpCacheService: HttpCacheService
	) {
	}
	/**
	 * 登录
	 * @param body
	 * @returns
	 */
	login(body): Promise<IHttpRes<ILogin>> {
		return this.apiService.post('/api/login?hasRole=true', body);
	}

	logout(): Promise<IHttpRes<any>> {
		return this.apiService.get('/api/logout');
	}

	saveUserInfo(userInfo) {
		this.httpCacheService.saveSessionCache(USER_INFO, JSON.stringify(userInfo));
	}

	getUserInfo() {
		const userInfo = this.httpCacheService.getSessionCache(USER_INFO);
		if (userInfo) {
			return JSON.parse(userInfo);
		}
	}

	clearUserInfo() {
		this.httpCacheService.removeSessionCache(USER_INFO);
		this.httpCacheService.removeSessionCache(USER_AUTH);

	}

	saveLoginInfo(loginInfo) {
		this.httpCacheService.saveSessionCache(USER_LOGIN, JSON.stringify(loginInfo));
	}

	getLoginInfo() {
		const loginInfo = this.httpCacheService.getSessionCache(USER_LOGIN);
		if (loginInfo) {
			return JSON.parse(loginInfo);
		}
	}
	removeLoginInfo() {
		this.httpCacheService.removeLocalCache(USER_LOGIN);
	}
	
}
